package cn.wolfcode.trip.admin.Controller;


import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 日志文章资源控制器
 */
@Controller
@RequestMapping("/new")
public class NewController {

    @Autowired
    private INewsService newsService;

    /**
     * 查出所有日志文章 黄子茵
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String query(@ModelAttribute("qo") NewsQueryObject qo, Model model){
        qo.setOrderBy("n.releaseTime desc");
        model.addAttribute("pageInfo",newsService.query(qo));
        return "/new/list";
    }

    /**
     * 日志文章保存和修改 黄子茵
     * @param news
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(News news){
        JSONResult jsonResult =new JSONResult();

        try {
            newsService.saveOrUpdate(news);
        } catch (Exception e) {
            jsonResult.mark("操作失败,请稍后再试");
            e.printStackTrace();
        }

        return jsonResult;
    }
}
